package cn.enjoy.boot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.locks.LockSupport;

/**
 * @author K
 * @date 2024-06-07 23:01
 */
@Slf4j
@RestController("asyn")
public class AsynInputController {

    private static HashMap<String, Thread> userThreadMap = new HashMap<>();
    private static HashMap<String, String> userInputMap = new HashMap<>();

    @GetMapping("connect")
    public void connect(String uid){
        userThreadMap.put(uid, Thread.currentThread());
        String inputId = UUID.randomUUID().toString();
        log.info("线程阻塞，等待唤醒 uid:{} inputId:{}", uid, inputId);
        LockSupport.park();
        log.info("线程唤醒，继续执行");
        log.info("用户输入值：{}", userInputMap.get(uid+ inputId));
    }

    @GetMapping("inputValue")
    public void inputValue(String uid, String inputId, String value){
        userInputMap.put(uid+inputId, value);
        Thread thread = userThreadMap.get(uid);


        LockSupport.unpark(thread);

    }

}
